package com.honeywen.mymvc.leetcode;

/**
 * 83. 删除排序链表中的重复元素
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
 * @author wangwei
 * @date 2018/11/6 上午9:17
 */
public class DuplicateListTest {

    public static void main(String[] args) {
        // 1,1,2,3,3

        ListNode head = new ListNode(1);
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        head.next = n1;
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;

        printListNode(head);

        printListNode(deleteDuplicates(head));
        printListNode(deleteDuplicates2(head));

    }

    private static void printListNode(ListNode listNode) {
       while (listNode != null) {
           System.out.print(listNode.val + "->");
           listNode = listNode.next;
       }
        System.out.println();
    }

    public static ListNode deleteDuplicates2(ListNode head) {

        ListNode flag = head;

        while (head != null && head.next != null) {

            if (head.val == head.next.val) {
                // 删除2
                head.next = head.next.next;

            } else {
                // 往前推进，
                head = head.next;
            }

        }


        return flag;

    }


    public static ListNode deleteDuplicates(ListNode head) {

        if (head == null) {
            return null;
        }

        ListNode node = new ListNode(head.val);
        ListNode x = node;

        while (head.next != null) {

            head = head.next;
            if (head.val > x.val) {
                x.next = new ListNode(head.val);
                x = x.next;
            }
        }

        return node;
    }



}
